/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.stress_view.command;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JFrame;

import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.ui.rich.learn.learn_controller.Controller;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdButton;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.DrawingArea;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.command.Command;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog.MatrixViewTableDialog;
import br.ufmg.dees.insane.util.IMatrix;


/**
*
* @author Moreira, Renata & Pitangueira, Roque
* @since May 2005
*/

public class SetFrameDMatrixCommand implements Command, Cloneable{
	private LearnInterface parent;
	private Controller controller;
	private DrawingArea da;
	
//*****************************************************************************
	
	/** Creates new SetElmAttributesCommand */
	public SetFrameDMatrixCommand(JFrame parent) {
		this.parent = (LearnInterface)parent;
		controller = this.parent.getView().getController();
		da = this.parent.getView().getDrawingArea();
	}
	
//*****************************************************************************
	
	/** Execute method. */
	public void execute() {
		
		ArrayList elms = controller.getSelection();
		//ArrayList elms = fmodel.getElementsList();
		if ( elms.size() < 1 ) {
			controller.selectAll();
			elms = controller.getSelection();
		}
		
		if (elms.size() == 0) {
			parent.addOutputText("There is no elements in model.");
			return;
		}
		
		ListIterator sels = elms.listIterator();
		while(sels.hasNext()) {
		FrameElement elm = (FrameElement)sels.next();
		
					
		IMatrix D = elm.getAnalysisModel().mountDMatrix(elm.getMaterial().getPs().toDoubleArray(), elm.getCrossSection());
					
//					Number Format
					DecimalFormat fmt = new DecimalFormat();
			        DecimalFormatSymbols fsym = new DecimalFormatSymbols();
			        fsym.setDecimalSeparator('.');
			        fmt.setDecimalFormatSymbols(fsym);
			        fmt.applyPattern("0.000E00");
			        
			        CmdButton[][] dMatrix = new CmdButton[D.getNumRow()][D.getNumCol()];
					for(int i =0; i< D.getNumRow(); i++)
						for(int j=0; j<D.getNumCol(); j++)
							dMatrix[i][j]=  new CmdButton(String.valueOf(fmt.format(D.getElement(i,j))));
					
					String[] colNames = new String[D.getNumCol()];
					for(int m=0; m<D.getNumCol(); m++){
					    colNames[m] = "   ";
					    
					}
					MatrixViewTableDialog dlg = new MatrixViewTableDialog("D Matrix", D.getNumRow());
					
					dlg.addOutputTable(elm.getLabel(),colNames,dMatrix);
					
					
			        dlg.showDialog();
					controller.unSelectAll();
					da.repaint();
	
				}
				
				
				
				
			}
	
	
//*****************************************************************************
	
	/** Impossible to undo - not implemented. */
	public void undo() {
	}
	
//*****************************************************************************
	
	/** Impossible to redo - not implemented. */
	public void redo() {
	}
	
//*****************************************************************************
	
	/** Clone method. */
	public Object clone() {
		return null;
	}
	
//*****************************************************************************


}

